package com.alliky.home.ui.fragment

import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alliky.common.entity.vo.BannerEntityInfo
import com.alliky.common.entity.vo.HomeHotVo
import com.alliky.common.entity.vo.HomeProductVo
import com.alliky.common.router.Router
import com.alliky.core.base.BaseFragment
import com.alliky.core.utils.ImageLoader
import com.alliky.home.R
import com.alliky.home.mvp.contract.HomeContract
import com.alliky.home.mvp.presenter.HomePresenter
import com.alliky.home.ui.adapter.HomeHotAdapter
import com.alliky.home.ui.adapter.HomeProductAdapter
import com.stx.xhb.androidx.XBanner
import kotlinx.android.synthetic.main.fragment_home.*
import kotlinx.android.synthetic.main.header_home_layout.*

@Route(path = Router.Home.Fragment.HomeFragment)
class HomeFragment : BaseFragment<HomeContract.View, HomeContract.Presenter>(),
    HomeContract.View {

    private var mHotAdapter: HomeHotAdapter? = null
    var mProductAdapter: HomeProductAdapter? = null

    override fun createPresenter(): HomeContract.Presenter = HomePresenter()

    override fun setLayout(): Any? = R.layout.fragment_home

    override fun initView() {
        super.initView()

        var headerView: View =
            LayoutInflater.from(requireContext()).inflate(R.layout.header_home_layout, null)

        var xbanner = headerView.findViewById<XBanner>(R.id.xbanner)
        var recyclerViewHots = headerView.findViewById<RecyclerView>(R.id.recyclerViewHots)
        xbanner.loadImage { _: XBanner, any: Any, view: View, _: Int ->
            ImageLoader.loadImage(
                requireContext(),
                (any as BannerEntityInfo).bannerEntity?.image,
                view as ImageView
            )
        }
        mHotAdapter = HomeHotAdapter(mutableListOf())
        recyclerViewHots.apply {
            layoutManager = GridLayoutManager(requireContext(), 4)
            adapter = mHotAdapter
        }

        mProductAdapter = HomeProductAdapter(mutableListOf())
        recyclerViewProduct.apply {
            layoutManager = GridLayoutManager(requireContext(), 2)
            adapter = mProductAdapter
        }

        mProductAdapter!!.addHeaderView(headerView)

    }

    override fun initData() {
        mPresenter?.getBanner(1)

        var hotDatas: MutableList<HomeHotVo> = ArrayList()
        hotDatas.add(HomeHotVo("天猫新品", R.mipmap.icon_phone))
        hotDatas.add(HomeHotVo("今日爆款", R.mipmap.icon_phone))
        hotDatas.add(HomeHotVo("天猫新品", R.mipmap.icon_phone))
        hotDatas.add(HomeHotVo("芭芭农场", R.mipmap.icon_phone))
        hotDatas.add(HomeHotVo("飞猪旅行", R.mipmap.icon_phone))
        hotDatas.add(HomeHotVo("天猫超市", R.mipmap.icon_phone))
        hotDatas.add(HomeHotVo("充值中心", R.mipmap.icon_phone))
        hotDatas.add(HomeHotVo("淘鲜达", R.mipmap.icon_phone))
//        hotDatas.add(HomeHotVo("天猫好药", R.mipmap.icon_phone))
//        hotDatas.add(HomeHotVo("土货鲜食", R.mipmap.icon_phone))
        mHotAdapter?.setNewInstance(hotDatas)

        var productList: MutableList<HomeProductVo> = ArrayList()
        productList.add(
            HomeProductVo(
                "[测试商品不发货] 耐克 男子 NIKE AIR FORCE 运动鞋",
                R.mipmap.icon_bijiben,
                169.98,
                388.99
            )
        )
        productList.add(
            HomeProductVo(
                "[测试商品不发货] 耐克 男子 NIKE AIR FORCE 运动鞋",
                R.mipmap.icon_phone,
                169.98,
                388.99
            )
        )
        productList.add(
            HomeProductVo(
                "[测试商品不发货] 耐克 男子 NIKE AIR FORCE 运动鞋",
                R.mipmap.icon_phone,
                169.98,
                388.99
            )
        )
        productList.add(
            HomeProductVo(
                "[测试商品不发货] 耐克 男子 NIKE AIR FORCE 运动鞋",
                R.mipmap.icon_bijiben,
                169.98,
                388.99
            )
        )
        productList.add(
            HomeProductVo(
                "[测试商品不发货] 耐克 男子 NIKE AIR FORCE 运动鞋",
                R.mipmap.icon_phone,
                169.98,
                388.99
            )
        )
        productList.add(
            HomeProductVo(
                "[测试商品不发货] 耐克 男子 NIKE AIR FORCE 运动鞋",
                R.mipmap.icon_phone,
                169.98,
                388.99
            )
        )
        productList.add(
            HomeProductVo(
                "[测试商品不发货] 耐克 男子 NIKE AIR FORCE 运动鞋",
                R.mipmap.icon_bijiben,
                169.98,
                388.99
            )
        )
        productList.add(
            HomeProductVo(
                "[测试商品不发货] 耐克 男子 NIKE AIR FORCE 运动鞋",
                R.mipmap.icon_bijiben,
                169.98,
                388.99
            )
        )
        productList.add(
            HomeProductVo(
                "[测试商品不发货] 耐克 男子 NIKE AIR FORCE 运动鞋",
                R.mipmap.icon_bijiben,
                169.98,
                388.99
            )
        )
        productList.add(
            HomeProductVo(
                "[测试商品不发货] 耐克 男子 NIKE AIR FORCE 运动鞋",
                R.mipmap.icon_bijiben,
                169.98,
                388.99
            )
        )
        productList.add(
            HomeProductVo(
                "[测试商品不发货] 耐克 男子 NIKE AIR FORCE 运动鞋",
                R.mipmap.icon_bijiben,
                169.98,
                388.99
            )
        )
        productList.add(
            HomeProductVo(
                "[测试商品不发货] 耐克 男子 NIKE AIR FORCE 运动鞋",
                R.mipmap.icon_bijiben,
                169.98,
                388.99
            )
        )

        mProductAdapter?.setNewInstance(productList)

    }

    override fun showBanner(list: List<BannerEntityInfo?>?) {
        if (list != null) {
            xbanner.setBannerData(list)
        }
    }
}